﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Collections;

namespace Ewts.Cumtomer
{
    public partial class CumAccounModify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dropDepart.DataSource = new DataLib.BLL.Cumtomer().GetAllList();
                dropDepart.DataTextField = "companyname";
                dropDepart.DataValueField = "id";
                dropDepart.DataBind();
               
                //BindSuppData();								
                User newuser;
                if (Request["userid"] != null)
                {
                    Role role = new Role();
                    chkLoadSearch.Value = role.GetRole("Load查询").RoleID.ToString();
                    chkGoodsStatus.Value = role.GetRole("货物状态").RoleID.ToString();
                    chkGoodsItems.Value = role.GetRole("原料明细").RoleID.ToString();

                    int userid = int.Parse(Request["userid"]);
                    newuser = new User(userid);
                    if (newuser == null)
                    {
                        Response.Write("<script language=javascript>window.alert('该用户不存在！\\');history.back();</script>");
                        return;
                    }
                    DataLib.Model.Cumtomer cum = new DataLib.BLL.cum_aacounts().GetCumByUserid(userid);
                    this.cumid.Value = cum.id.ToString();

                    this.lblName.Text = newuser.UserName;
                    this.txtPassword.Text = "";
                    txtTrueName.Text = newuser.TrueName;
                    this.txtPhone.Text = newuser.Phone;
                    txtEmail.Text = newuser.Email;
                    dropDepart.SelectedValue = cum.id.ToString();

                  
                    if (newuser.IsExitsRole(newuser.UserID,"货物状态"))
                    {
                        chkGoodsItems.Checked = true;
                    }
                    if (newuser.IsExitsRole(newuser.UserID, "原料明细"))
                    {
                        chkGoodsStatus.Checked = true;
                    }
                    if (newuser.IsExitsRole(newuser.UserID, "Load查询"))
                    {
                        chkLoadSearch.Checked = true;
                    }
                 

                    //dropUserType.SelectedValue = currentUser.Duty;
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            /*
            dropDepart.DataSource = new DataLib.BLL.Cumtomer().GetAllList();
            dropDepart.DataTextField = "companyname";
            dropDepart.DataValueField = "id";
            */

            int cum = int.Parse(this.cumid.Value);
            User currentUser = new User(lblName.Text);
            DataLib.BLL.cum_aacounts cumbll = new DataLib.BLL.cum_aacounts();
            DataLib.Model.cum_aacounts cumacc =cumbll.GetModel(cum,currentUser.UserID);
            if(this.txtPassword.Text.Trim() == "")
            {

            }
            else
            {
               currentUser.Password = AccountsPrincipal.EncryptPassword(txtPassword.Text);
            }
            currentUser.TrueName = this.txtTrueName.Text.ToString();
            currentUser.Phone = this.txtPhone.Text.ToString();
            currentUser.Email = this.txtEmail.Text.ToString();
            //currentUser.Duty =dropUserType.SelectedValue;
            cumacc.cumid = int.Parse(this.dropDepart.SelectedValue);
            if (chkGoodsItems.Checked)
            {
                currentUser.AddToRole(int.Parse(chkGoodsItems.Value));
            }
            if (chkGoodsStatus.Checked)
            {
                currentUser.AddToRole(int.Parse(chkGoodsStatus.Value));
            }
            if (chkLoadSearch.Checked)
            {
                currentUser.AddToRole(int.Parse(chkLoadSearch.Value));
            }
  
            bool a = cumbll.Update(cumacc);
            bool b = currentUser.Update();
            if (a&b == false)
            {
                this.lblMsg.ForeColor = Color.Red;
                this.lblMsg.Text = "更新用户信息发生错误！";
            }
            else
            {
                Response.Redirect("CumAccountList.aspx");
            }
        }
    }
}
